﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class PersonnelEditViewModel : NotificationObject
    {
        private PersonnelEdit gWindow;
        private PersonnelViewModel gPersonnelViewModel;
        private CPersonnel gPersonnel;

        public PersonnelEditViewModel(PersonnelEdit pWindow, PersonnelViewModel pPersonnelViewModel, CPersonnel pPersonnel)
        {
            gWindow = pWindow;
            gPersonnelViewModel = pPersonnelViewModel;
            gPersonnel = pPersonnel;

            Initialize();
        }

        public void Initialize()
        {
            Name = gPersonnel.Name;
            Username = gPersonnel.Username;
            Password = gPersonnel.Password;
            PasswordEnter = gPersonnel.Password;
            ID = gPersonnel.ID;
            Birthday = gPersonnel.Birthday;
            Phone = gPersonnel.Phone;
            Emergency = gPersonnel.Emergency;
            WebChat = gPersonnel.WebChat;

            HttpResponse response = API.SelectPersonnelGroup();
            if(response.S == HttpResponse.Success)
            {
                PersonnelGroup = JSON.parse<ObservableCollection<CPersonnelGroup>>(response.D);
            }
            PersonnelGroupUID = gPersonnel.PersonnelGroupUID;

            Sex.Add("男");
            Sex.Add("女");
            SexSelect = gPersonnel.SexName;

            response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
            ProvinceUID = gPersonnel.ProvinceUID;

            response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                City = JSON.parse<ObservableCollection<CCity>>(response.D);
            }
            CityUID = gPersonnel.CityUID;
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private String username;
        public String Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
                RaisePropertyChanged("Username");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }

            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private String passwordEnter;
        public String PasswordEnter
        {
            get
            {
                return passwordEnter;
            }

            set
            {
                passwordEnter = value;
                RaisePropertyChanged("PasswordEnter");
            }
        }

        private ObservableCollection<CPersonnelGroup> personnelGroup;
        public ObservableCollection<CPersonnelGroup> PersonnelGroup
        {
            get
            {
                return personnelGroup;
            }

            set
            {
                personnelGroup = value;
                RaisePropertyChanged("PersonnelGroup");
            }
        }

        private int personnelGroupUID;
        public int PersonnelGroupUID
        {
            get
            {
                return personnelGroupUID;
            }

            set
            {
                personnelGroupUID = value;
                RaisePropertyChanged("PersonnelGroupUID");
            }
        }

        private List<String> sex = new List<string>();
        public List<String> Sex
        {
            get
            {
                return sex;
            }

            set
            {
                sex = value;
                RaisePropertyChanged("Sex");
            }
        }

        private String sexSelect;
        public String SexSelect
        {
            get
            {
                return sexSelect;
            }

            set
            {
                sexSelect = value;
                RaisePropertyChanged("SexSelect");
            }
        }

        private String id;
        public String ID
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
                RaisePropertyChanged("ID");
            }
        }

        private String birthday;
        public String Birthday
        {
            get
            {
                return birthday;
            }

            set
            {
                birthday = value;
                RaisePropertyChanged("Birthday");
            }
        }

        private String phone;
        public String Phone
        {
            get
            {
                return phone;
            }

            set
            {
                phone = value;
                RaisePropertyChanged("Phone");
            }
        }

        private String emergency;
        public String Emergency
        {
            get
            {
                return emergency;
            }

            set
            {
                emergency = value;
                RaisePropertyChanged("Emergency");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CCity> city = new ObservableCollection<CCity>();
        public ObservableCollection<CCity> City
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        private int cityUID;
        public int CityUID
        {
            get
            {
                return cityUID;
            }

            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        private String webChat;
        public String WebChat
        {
            get
            {
                return webChat;
            }

            set
            {
                webChat = value;
                RaisePropertyChanged("WebChat");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            int sex = 0;
            if (SexSelect == "男") sex = 0;
            else sex = 1;

            String Time = Convert.ToDateTime(Birthday).ToString("yyyy/MM/dd");
            API.UpdatePersonnel(new CPersonnel(Name, Username, Password, "", PersonnelGroupUID, sex, ID, Time, Phone, Emergency, CityUID, WebChat), gPersonnel.UID);
            gPersonnelViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            City.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                City = JSON.parse<ObservableCollection<CCity>>(response.D);
            }
        }
    }
}
